package com.mmall.concurrency.thread.one;

import java.util.Queue;

/**
 * 功能描述：
 *
 * @author 吴明泽
 * @version 1.0
 * @date 2022-10-20 19:49:48
 */
public class Consumer implements Runnable {

    private Queue<String> bags;

    private int maxSize;

    public Consumer(Queue<String> bags, int maxSize) {
        this.bags = bags;
        this.maxSize = maxSize;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (bags) {
                if (bags.isEmpty()) {
                    System.out.println("bags为空");
                    try {
                        bags.wait(); // 阻塞等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    // 作用：让出时间片
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 不为空，可以消费
                final String bag = bags.remove();
                System.out.println("消费者消费：" + bag);
                bags.notify();
            }
        }
    }

}
